<?php

namespace App\Admin\Controllers;

use App\Models\UserManagementModel;
use App\Admin\Table\UserManagementTable;
use App\Http\Controllers\Controller;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class ManagementController extends Controller
{
    private UserManagementTable $user;
    private UserManagementModel $userManagementModel;

    public function __construct()
    {
        $this->user = new UserManagementTable();
        $this->userManagementModel = new UserManagementModel();
    }

    /**
     * 表格展示页面
     * @param Content $content
     * @return Content
     */
    protected function grid(Content $content): Content
    {
        return $content
            ->header(admin_trans('global.user_management')) // 标题
            ->description(admin_trans('admin.list'))
            ->body($this->user->grid());
    }

    /**
     * 新增表单页面
     * @param Content $content
     * @return Content
     */
    protected function addForm(Content $content): Content
    {
        return $content
            ->header(admin_trans('global.user_management')) // 标题
            ->description(admin_trans('admin.create')) // 介绍
            ->body($this->user->addForm());
    }

    /**
     * 新增接口
     * @param Request $request
     * @return JsonResponse
     */
    protected function store(Request $request): JsonResponse
    {
//        过滤掉不需要保存的字段
        $array = Arr::only($request->all(), ['name', 'email', 'password', 'state']);
        foreach ($array as $key => $value) {
            $this->userManagementModel->$key = $value;
        }
        $isSave = $this->userManagementModel->save();
        if ($isSave) {
            return JsonResponse::make()->success('保存成功')->redirect('/user_management');
        }
        return JsonResponse::make()->success('保存失败');
    }

    /**
     * 查看详情
     * @param Content $content
     * @param $id
     * @return Content
     */
    protected function detail(Content $content, $id): Content
    {
        return $content
            ->header(admin_trans('global.user_management')) // 标题
            ->description(admin_trans('admin.view')) // 介绍
            ->body($this->user->detail($id));
    }

    /**
     * 编辑表单页面
     * @param Content $content
     * @param $id
     * @return Content
     */
    protected function editForm(Content $content, $id): Content
    {
        return $content
            ->header(admin_trans('global.user_management')) // 标题
            ->description(admin_trans('admin.edit')) // 介绍
            ->body($this->user->editForm($id));
    }

    /**
     * 编辑接口
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    protected function edit(Request $request, $id)
    {
        $array = Arr::only($request->all(), ['name', 'email', 'password', 'state']);
        $data = $this->userManagementModel::find($id);
        foreach ($array as $key => $value) {
            $data->$key = $value;
        }
        $isSave = $data->save();
        if ($isSave) {
            return JsonResponse::make()->success('修改成功')->redirect('/user_management');
        }
        return JsonResponse::make()->success('修改失败');
    }

    protected function delete(Request $request, $idStr)
    {
//        explode(',', $idStr)字符串分割，返回一个数组
        $isDeleted = UserManagementModel::destroy(explode(',', $idStr));
        if ($isDeleted) {
            return JsonResponse::make()->success('删除成功');
        }
        return JsonResponse::make()->error('删除失败');
    }
}
